package view;

import controller.DataInputControl;
import entity.SystemM;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class DataInputView extends javax.swing.JFrame {

    public List<SystemM> register = new ArrayList<>();
    JFrame frame;

    public DataInputView(List<SystemM> Register) {
        initComponents();
        register = Register;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        returnBtn = new javax.swing.JButton();
        tempTxt = new javax.swing.JTextField();
        pressionTxt = new javax.swing.JTextField();
        pluviosityTxt = new javax.swing.JTextField();
        IngresarBtn = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Temperatura:");

        jLabel2.setText("Presion atmosférica:");

        jLabel3.setText("Pluviosidad:");

        returnBtn.setText("Volver");
        returnBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnBtnActionPerformed(evt);
            }
        });

        IngresarBtn.setText("Ingresar datos");
        IngresarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IngresarBtnActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("URW Bookman L", 3, 15)); // NOI18N
        jLabel4.setForeground(java.awt.Color.blue);
        jLabel4.setText("Central meteorológica AguasCalientes");

        jLabel5.setFont(new java.awt.Font("URW Bookman L", 3, 15)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 255));
        jLabel5.setText("Ingreso de Datos");
        jLabel5.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(40, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(122, 122, 122))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(returnBtn, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(IngresarBtn, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                    .addComponent(pluviosityTxt)
                    .addComponent(tempTxt)
                    .addComponent(pressionTxt))
                .addGap(42, 42, 42))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tempTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(pressionTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(pluviosityTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(returnBtn)
                    .addComponent(IngresarBtn))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void IngresarBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IngresarBtnActionPerformed
        DataInputControl control = new DataInputControl(register);
        if (validPluviosityTxt() == true && validTempTxt() == true && validPressionTxt() == true) {
            float temp = Float.valueOf(tempTxt.getText().trim()).floatValue();
            float pression = Float.valueOf(pressionTxt.getText().trim()).floatValue();
            float publiosity = Float.valueOf(pluviosityTxt.getText().trim()).floatValue();
            System.out.println(control.inputData(temp, pression, publiosity));
            pluviosityTxt.setText(null);
            tempTxt.setText(null);
            pressionTxt.setText(null);
        } else {
            JOptionPane.showMessageDialog(frame, "Datos Incorrectos");            
        }

    }//GEN-LAST:event_IngresarBtnActionPerformed

    private void returnBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_returnBtnActionPerformed
        CentralFrame newCentralFrame = new CentralFrame();
        newCentralFrame.setLocationRelativeTo(this);
        newCentralFrame.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_returnBtnActionPerformed
    private boolean validTempTxt() {
        boolean flag = true;
        try {
            Float.parseFloat(tempTxt.getText());
        } catch (NumberFormatException ex) {
            flag = false;
        }
        return (!tempTxt.getText().isEmpty() && flag);
    }

    private boolean validPressionTxt() {
        boolean flag = true;
        try {
            Float.parseFloat(pressionTxt.getText());
        } catch (NumberFormatException ex) {
            flag = false;
        }
        return (!tempTxt.getText().isEmpty() && flag);
    }

    private boolean validPluviosityTxt() {
        boolean flag = true;
        try {
            Float.parseFloat(pluviosityTxt.getText());
        } catch (NumberFormatException ex) {
            flag = false;
        }
        return (!tempTxt.getText().isEmpty() && flag);
    }
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton IngresarBtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField pluviosityTxt;
    private javax.swing.JTextField pressionTxt;
    private javax.swing.JButton returnBtn;
    private javax.swing.JTextField tempTxt;
    // End of variables declaration//GEN-END:variables
}
